<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<HTML>
<HEAD>
<c:import url="/includes/header.jsp"></c:import>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/js/lib/jquery-tree/css/demo.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/js/lib/jquery-tree/css/zTreeStyle.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/lib/jquery-tree/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/lib/jquery-tree/js/jquery.ztree.excheck-3.5.js"></script>
</HEAD>
<BODY>
	<!-- 权限管理 -->
	<div class="modal-header">
		<input id="userId" name="userId" type="hidden" value="${userId}">
		<button type="button" class="close" data-dismiss="modal"
			aria-label="Close">
			<span aria-hidden="true">&times;</span>
		</button>
		<h4 class="modal-title" id="myModalLabel">设置权限</h4>
	</div>
	<div class="content_wrap">
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		<div class="right">
			<ul class="info">
				<li class="title"><h2>注意:</h2>
					<ul class="list">
						<li class="highlight_red">请管理员谨慎分配其他管理员权限,以确保系统数据的安全性</li>
						<li class="highlight_red">A对B授权,B对C授权,若A取消对B的授权时,则C的对应权限同时被取消</li>
					</ul></li>
			</ul>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
		<button type="button" class="btn btn-primary btn-save">保存</button>
	</div>
</BODY>
</HTML>

<script type="text/javascript">
	$(function() {
		var setting = {
			check : {
				enable : true,
				chkboxType : {
					"Y" : "ps",
					"N" : "ps"
				}

			},
			data : {
				simpleData : {
					enable : true,
					idKey : "id",
					pIdKey : "pId",
					rootPId : ""
				}
			}
		};
		
		var userId = $("#userId").val();
		var url = "${pageContext.request.contextPath}/admin/resource/tree.do";
		$.ajax({
			url : url,
			type : "post",
			dataType : "json",
			data : {
				"userId" : userId
			},
			async : true,
			success : function(data) {
				if (data.code == "0") {
					console.info(data.data);
					var zNodesTree = data.data;
					var zTree = $("#treeDemo")
					$.fn.zTree.init(zTree, setting, zNodesTree);
				}
			},
			error : function() {
				bootbox.alert("网络不稳定,保存失败");
			}
		});

		//保存
		$(".btn-save")
				.click(
						function() {
							var userId = $("#userId").val();
							var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
							var nodes = treeObj.getCheckedNodes(true);
							var ids = new Array();
							$.each(nodes, function(n, value) {
								if (value) {
									ids.push(value.id);
								}
							});
							var resources = ids.join(",");
							var url = "${pageContext.request.contextPath}/admin/resource/privilege/save.do";
							$.ajax({
								url : url,
								type : "post",
								dataType : "json",
								data : {
									"userId" : userId,
									"resourceIds" : resources
								},
								async : true,
								success : function(data) {
									if (data.code == "0") {
										myModalHidden();
										bootbox.alert("保存成功");
									} else {
										bootbox.alert("网络不稳定,保存失败");
									}
								},
								error : function() {
									bootbox.alert("网络不稳定,保存失败");
								}
							});
						});

		var myModalHidden = function() {
			$('#myModal').hide();
			$(".modal-backdrop").remove();
		}

	});
</script>